package cz.byteworks.android.myway.location;

import java.util.TimerTask;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import cz.byteworks.android.myway.db.DataHelper;

public class LocTimerTask extends TimerTask {
	
	private PositionService positionService;
	int origPeriod;
	Context _ctx;
	
	public LocTimerTask(Context ctx, DataHelper dataHelper, int period) {
		this.positionService = new PositionService(ctx, dataHelper);
		origPeriod = period;
		_ctx = ctx;
	}

	@Override
	public void run() {
		if (!AutoLocator.isLocating()) {
			positionService.storeActualPosition();
		}
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(_ctx);
		if (new Integer(prefs.getString("prefPeriodKey", "60")) != origPeriod) {
			AutoLocator.stopAutolocation(_ctx);
			AutoLocator.startAutolocation(_ctx, new DataHelper(_ctx), new Integer(prefs.getString("prefPeriodKey", "60")));
		}
	}

}
